/**
 * @author 黄波
 * 创建日期：2011Sep 7, 2011
 * TODO// N/A
 */
package com.platform.user.action;

import java.util.ArrayList;
import java.util.List;

import com.platform.bean.PageBean;
import com.platform.common.bo.CommonBo;
import com.platform.os.util.Util;
import com.platform.pojo.UserGroup;
import com.platform.pojo.UserInfo;
import com.platform.user.bean.AccountInfo;
import com.platform.user.bo.UserBo;

/**
 * 分页加载用户信息
 */
public class LoadUsersAction extends UserSupport {

	private PageBean pageBean;	//分页参数
	private List<AccountInfo> accounts;	//用户列表
	private String id;	//树节点id
	private String nodeText;	//树节点名称
	/**
	 * @return the pageBean
	 */
	public PageBean getPageBean() {
		return pageBean;
	}
	/**
	 * @param pageBean the pageBean to set
	 */
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	/**
	 * @return the accounts
	 */
	public List<AccountInfo> getAccounts() {
		return accounts;
	}
	/**
	 * @param accounts the accounts to set
	 */
	public void setAccounts(List<AccountInfo> accounts) {
		this.accounts = accounts;
	}
	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * @return the nodeText
	 */
	public String getNodeText() {
		return nodeText;
	}
	/**
	 * @param nodeText the nodeText to set
	 */
	public void setNodeText(String nodeText) {
		this.nodeText = nodeText;
	}
	private List<UserGroup> groups;
	
	/**
	 * @return the groups
	 */
	public List<UserGroup> getGroups() {
		return groups;
	}
	/**
	 * @param groups the groups to set
	 */
	public void setGroups(List<UserGroup> groups) {
		this.groups = groups;
	}
	@Override
	public String execute() throws Exception {
		UserBo bo = UserBo.getInstance();
		groups = bo.getGroups();
		Object[] objs = bo.getUserList(pageBean);
		pageBean = (PageBean)objs[0];
		List<UserInfo> users = (List)objs[1];
		//将users信息转换旋转到account list中，方便前台显示
		for(UserInfo user : users) {
			AccountInfo account = new AccountInfo();
			account.setUser(user);
			account.setRegTime(Util.parseDate(user.getRegTime(), "yyyy-MM-dd HH:mm:ss"));
			account.setLastLoginTime(Util.parseDate(user.getLastLoginTime(), "yyyy-MM-dd HH:mm:ss"));
			if(user.getGroupId() != null || user.getGroupId().intValue() != 0) {
				account.setGroup(bo.getUserGroup(user.getGroupId().toString()));
			} 
			if(accounts == null) {
				accounts = new ArrayList<AccountInfo>();
			}
			if(user.getDepartmentId() != null) {
				account.setDepartment(CommonBo.getInstance().getSchoolProperty(user.getDepartmentId().toString()));
			}
			if(user.getProfessionalId() != null) {
				account.setProfessional(CommonBo.getInstance().getSchoolProperty(user.getProfessionalId().toString()));
			}
			if(user.getGradeId() != null) {
				account.setGrade(CommonBo.getInstance().getSchoolProperty(user.getGradeId().toString()));
			}
			if(user.getClassId() != null) {
				account.setClazz(CommonBo.getInstance().getSchoolProperty(user.getClassId().toString()));
			}
			accounts.add(account);
		}
		if(nodeText != null) {
			nodeText = new String(nodeText.getBytes("iso-8859-1"), "utf-8");
		}
		return SUCCESS;
	}
	/**
	 * @param groupId
	 * @return
	 */
	private String getGroupName(Integer groupId) {
		if(groups != null) {
			for(UserGroup group : groups) {
				if(group.getId() == groupId) {
					return group.getGroupName();
				}
			}
		}
		return "普通用户组";
	}
	
}
